package com.shangyueshang.controller.sys;

import com.shangyueshang.request.IdDTO;
import com.shangyueshang.request.PageReq;
import com.shangyueshang.request.SysDictDTO;
import com.shangyueshang.response.HttpResult;
import com.shangyueshang.service.SysDictService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/5 17:41
 * @version: v1.0.0
 */
@RestController
@RequestMapping("/dict")
public class SysDictController {

    @Resource
    private SysDictService sysDictService;

    /**
     * 保存字典
     *
     * @param dictDTO
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('sys:dict:add') and hasAuthority('sys:dict:edit')")
    public HttpResult save(@RequestBody SysDictDTO dictDTO) {
        return HttpResult.ok(sysDictService.saveUpdate(dictDTO));
    }

    /**
     * 删除字典
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('sys:dict:delete')")
    public HttpResult delete(@NotEmpty @RequestBody List<IdDTO> ids) {
        return HttpResult.ok(sysDictService.removeByIds(ids.stream().map(IdDTO::getId).collect(Collectors.toList())));
    }

    /**
     * 分页查询
     *
     * @param pageReq
     * @return
     */
    @PostMapping("/findPage")
    //  @PreAuthorize("hasAuthority('sys:dict:view')")
    public HttpResult findPage(@RequestBody PageReq pageReq) {
        return HttpResult.ok(sysDictService.findPage(pageReq));
    }

    /**
     * 根据标签名查询
     *
     * @param label
     * @return
     */
    @GetMapping("/findByLabel")
    //   @PreAuthorize("hasAuthority('sys:dict:view')")
    public HttpResult findByLabel(@RequestParam String label) {
        return HttpResult.ok(sysDictService.findByLabel(label));
    }

    /**
     * 根据类型查询
     *
     * @param type
     * @return
     */
    @GetMapping("/findByType")
    //  @PreAuthorize("hasAuthority('sys:dict:view')")
    public HttpResult findByType(@RequestParam String type) {
        return HttpResult.ok(sysDictService.findByType(type));
    }

    /**
     * 根据类型查询
     *
     * @param types
     * @return
     */
    @PostMapping("/findByTypes")
    //  @PreAuthorize("hasAuthority('sys:dict:view')")
    public HttpResult findByTypes(@NotEmpty @RequestBody List<String> types) {
        return HttpResult.ok(sysDictService.findByTypes(types));
    }
}
